<?php
include_once ("includes/init.php");
include_once ("includes/match_utils.php");

/* Requires logged in user */
if (!isset ($loggedin)) {
    $message = "Permission Denied";
    include ("includes/redirect.php");
    return;
}

$noSeasonChange = true;
$image = $season->getImage();
$title = $season->season . " Match Entry";
include_once ("includes/header.php");

$submit = getParam("submit");
$home_team = getParam("home_team");
$home_td = getParam("home_td");
$home_cas = getParam("home_cas");
$away_team = getParam("away_team");
$away_td = getParam("away_td");
$away_cas = getParam("away_cas");
$gate = getParam("gate");
$matchType = getParam("matchType");
$year = getParam("year");
$month = getParam("month");
$day = getParam("day");
if (isset ($submit)) {
    $md = new MatchDao();
    $rs = $md->addMatch($home_team, $away_team, $home_td, $away_td, $home_cas, $away_cas, $gate, $matchType, $year, $month, $day);
    if ($rs) {
        $redirect = "match_edit.php?reload=true&matchId=" . $rs;
        $redirectStr = "Match Details";
        $message = "Update the match details";
        include ("includes/redirect.php");
        return;
    } else {
        echo "<tr><td>Unable to add match</td></tr>";
    }
}

$day = date("j");
$month = date("m");
$year = date("Y");

$months = array (
    "Jan",
    "Feb",
    "Mar",
    "Apr",
    "May",
    "Jun",
    "Jul",
    "Aug",
    "Sep",
    "Oct",
    "Nov",
    "Dec"
);

$home_team = 0;
$away_team = 0;
if (hasFixtures($season)) {
    $fixtures = loadFixtures($season, $teams);
    foreach ($fixtures as $fixture) {
    	if ($fixture->homeTeam->coach->id == $loggedin->id or $fixture->awayTeam->coach->id == $loggedin->id) {
		    $home_team = $fixture->homeTeam->id;
		    $away_team = $fixture->awayTeam->id;
		    break;
		}		
    }
}

echo<<<EOQ
<caption><h1>Submit Results</h1><br /><h3>$season->season $image</h3></caption>
<form action="match_create.php" method="POST">
<tr>
  <th>&nbsp;</th>
  <th>&nbsp;</th>
  <th>TD</th>
  <th>Cas</th>
  <th>Gate</th>
  <th>Type</th>
</tr>
<tr>
  <th>Home Player</th>
EOQ;
echoTeamSelect("home_team", $teams, $home_team, $loggedin->id);
echoSelect("home_td", 17, $home_td);
echoSelect("home_cas", 17, $home_cas);
echo<<<EOQ
  <td rowspan="2" align="center" valign="middle">
    <input type="text" name="gate" value="14000" maxlength="8" size="8"/>
  </td>
  <td rowspan="2" align="center" valign="middle">
    <select name="matchType">
      <option value="League" selected>League</option>
      <option value="Semi-Final">Semi-Final</option>
      <option value="Final">Final</option>
      <option value="Other">Other</option>
    </select>
  </td>
</tr>
<tr class="row2">
  <th>Away Player</th>
EOQ;
echoTeamSelect("away_team", $teams, $away_team, $loggedin->id);
echoSelect("away_td", 17, $away_td);
echoSelect("away_cas", 17, $away_cas);
echo<<<EOQ
</tr>
<tr>
  <td colspan="8" align="center">
EOQ;
include ("includes/date.php");
echo<<< EOQ
  </td>
</tr>
<tr><td colspan=8 align="center"><input type="submit" name="submit" value="submit"></td></tr>
EOQ;

include ("includes/footer.php");
?>